"use strict";

module.exports = {
  // 封装返回json对象
  json(data, code, msg, addition) {
    return Object.assign(
      {
        suc: code && code == 200 ? true : false,
        code: code || 0,
        message: msg,
        data,
      },
      addition
    );
  },
  parseInt(string) {
    if (typeof string === "number") return string;
    if (!string) return string;
    return parseInt(string) || 0;
  },
  //将js的date转化为mysql中的datatime
  parseTime(date) {
    /* return date.toISOString().slice(0, 19).replace("T", " "); */
    return date.getFullYear() + '-' +
    ('0' + (date.getMonth() + 1)).slice(-2) + '-' +
    ('0' + date.getDate()).slice(-2) + ' ' +
    ('0' + date.getHours()).slice(-2) + ':' +
    ('0' + date.getMinutes()).slice(-2) + ':' +
    ('0' + date.getSeconds()).slice(-2);
  },
  //获得一个随机字符串作为班级code
  getRandomCode() {
    let str = '';
    do{
      str = Math.random().toString(36).slice(-8);
    }while(str === '' || str === null)
    return str;
  },
  /**
   * @description: 将mysql的抛错调整为便于阅读的消息并返回
   * @param {*} error
   * @return {*}
   */  
  mysqlErrorToMsg(error){
    console.log(error);
    
  },
  getRndInteger(min, max) {
    return Math.floor(Math.random() * (max - min) ) + min;
  }
};
